package com.joysuch.wwyt.api.basedata.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.joysuch.wwyt.api.basedata.bean.*;
import com.joysuch.wwyt.api.basedata.service.BpDepartSyncService;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.dzhapi.bean.ApiBaseConfigBean;
import com.joysuch.wwyt.dzhapi.bean.ApiBaseUserBean;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.mapper.ApiBaseConfigMapper;
import com.joysuch.wwyt.dzhapi.mapper.ApiBaseUserMapper;
import com.joysuch.wwyt.monitoringcenter.bean.LocateLoginRespDto;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import com.joysuch.wwyt.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BpDepartSyncServiceImpl implements BpDepartSyncService {


    @Autowired
    private BaseConfigService baseConfigService;
    @Autowired
    private LocateServerService locateServerService;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private ApiBaseConfigMapper apiBaseConfigMapper;
    @Autowired
    private ApiBaseUserMapper userMapper;


    @Override
    public boolean addSync(BpDepart entity, BpDepart parent) {

        log.info("部门新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync(entity.getTenentId(), entity.getOrgCode().substring(0, 7))) {
            return false;
        }
        BpDepartSyncRequestBean requestBean = new BpDepartSyncRequestBean();
        BaseConfig buildConfig = baseConfigService.findConfig(entity.getTenentId(), entity.getOrgCode().substring(0, 7), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpDepartSyncAddBean> beans = new ArrayList<>();
        BpDepartSyncAddBean bean = new BpDepartSyncAddBean();
        bean.setDepartName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setParentName(parent.getName());
        bean.setParentUniqueId(parent.getUniqueId());
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (obu.isPresent()) {
                if (StringUtils.isNotBlank(obu.get().getSn()) && obu.get().getUniqueId() != null) {
                    bean.setChiefUniqueId(obu.get().getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        beans.add(bean);
        requestBean.setBuildId(buildConfig.getValue());
        requestBean.setData(beans);
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(requestBean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_ADD, LocateLoginRespDto.class);
            if (response == null) {
                log.info("部门新增同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("部门新增同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("部门新增同步成功 code--" + entity.getCode());
                        flag = true;
                    } else {
                        log.error("部门新增同步异常:code= " + entity.getCode());
                    }
                }
            }
        } catch (Exception e) {
            log.error("部门新增同步异常 e{}", e);
        }
        log.info("部门新增同步结束........");
        return flag;
    }

    @Override
    public boolean addApiSync(BpDepart entity, BpDepart parent, BuildDataDto dto) {

        log.info("部门新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        BpDepartSyncRequestBean requestBean = new BpDepartSyncRequestBean();
        ApiBaseConfigBean buildConfig = apiBaseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpDepartSyncAddBean> beans = new ArrayList<>();
        BpDepartSyncAddBean bean = new BpDepartSyncAddBean();
        bean.setDepartName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setParentName(parent.getName());
        bean.setParentUniqueId(parent.getUniqueId());
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            ApiBaseUserBean userBean = userMapper.selectById(entity.getChiefId());
//            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (userBean != null) {
                if (StringUtils.isNotBlank(userBean.getSn()) && userBean.getUniqueId() != null) {
                    bean.setChiefUniqueId(userBean.getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        beans.add(bean);
        requestBean.setBuildId(buildConfig.getValue());
        requestBean.setData(beans);
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(requestBean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_ADD, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.info("部门新增同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("部门新增同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("部门新增同步成功 code--" + entity.getCode());
                        flag = true;
                    } else {
                        log.error("部门新增同步异常:code= " + entity.getCode());
                    }
                }
            }
        } catch (Exception e) {
            log.error("部门新增同步异常 e{}", e);
        }
        log.info("部门新增同步结束........");
        return flag;
    }

    public boolean addSyncCross(BpDepart entity, BpDepart parent, String buildId) {

        log.info("部门新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BpDepartSyncRequestBean requestBean = new BpDepartSyncRequestBean();
//        BaseConfig buildConfig = baseConfigService.findConfig(entity.getTenentId(), entity.getOrgCode().substring(0, 7), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("部门新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpDepartSyncAddBean> beans = new ArrayList<>();
        BpDepartSyncAddBean bean = new BpDepartSyncAddBean();
        bean.setDepartName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setParentName(parent.getName());
        bean.setParentUniqueId(parent.getUniqueId());
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (obu.isPresent()) {
                if (StringUtils.isNotBlank(obu.get().getSn()) && obu.get().getUniqueId() != null) {
                    bean.setChiefUniqueId(obu.get().getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        beans.add(bean);
        requestBean.setBuildId(buildId);
        requestBean.setData(beans);
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(requestBean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_ADD, LocateLoginRespDto.class, entity.getTenentId(), entity.getOrgCode(), buildId);
        if (response == null) {
            log.info("部门新增同步异常：response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.error("部门新增同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
            } else {
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                    log.info("部门新增同步成功 code--" + entity.getCode());
                    flag = true;
                } else {
                    log.error("部门新增同步异常:code= " + entity.getCode());
                }
            }
        }
        log.info("部门新增同步结束........");
        return flag;
    }

    @Override
    public boolean updateSync(BpDepart entity, BpDepart parent) {
        log.info("部门修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync(entity.getTenentId(), entity.getOrgCode().substring(0, 7))) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        if (parent != null) {
            bean.setParentUniqueId(parent.getUniqueId());
        }
        bean.setBuildId(buildConfig.getValue());
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (obu.isPresent()) {
                if (StringUtils.isNotBlank(obu.get().getSn()) && obu.get().getUniqueId() != null) {
                    bean.setChiefUniqueId(obu.get().getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_UPDATE, LocateLoginRespDto.class);
            if (response == null) {
                log.error("部门修改同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("部门修改同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("部门修改同步成功 code={}, uniqueId={}" , entity.getCode(), entity.getUniqueId());
                        flag = true;
                    } else {
                        log.error("部门修改同步异常:code= " + entity.getCode());
                    }
                }
            }
        } catch (Exception e) {
            log.error("部门修改同步异常 e{}", e);
        }
        log.info("部门修改同步结束........");
        return flag;
    }

    @Override
    public boolean updateApiSync(BpDepart entity, BpDepart parent, BuildDataDto dto) {
        log.info("部门修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = apiBaseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        if (parent != null) {
            bean.setParentUniqueId(parent.getUniqueId());
        }
        bean.setBuildId(buildConfig.getValue());
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            ApiBaseUserBean userBean = userMapper.selectById(entity.getChiefId());
//            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (userBean != null) {
                if (StringUtils.isNotBlank(userBean.getSn()) && userBean.getUniqueId() != null) {
                    bean.setChiefUniqueId(userBean.getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_UPDATE, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.error("部门修改同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("部门修改同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("部门修改同步成功 code--" + entity.getCode());
                        flag = true;
                    } else {
                        log.error("部门修改同步异常:code= " + entity.getCode());
                    }
                }
            }
        } catch (Exception e) {
            log.error("部门修改同步异常 e{}", e);
        }
        log.info("部门修改同步结束........");
        return flag;
    }

    public boolean updateSyncCross(BpDepart entity, BpDepart parent, String buildId) {
        log.info("部门修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync(entity.getTenentId(), entity.getOrgCode().substring(0, 7))) {
            return false;
        }
//        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("部门修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        if (parent != null) {
            bean.setParentUniqueId(parent.getUniqueId());
        }
        bean.setBuildId(buildId);
        bean.setRemark(entity.getRemark());
        if (entity.getChiefId() != null) {
            Optional<BaseUser> obu = baseUserDao.findById(entity.getChiefId());
            if (obu.isPresent()) {
                if (StringUtils.isNotBlank(obu.get().getSn()) && obu.get().getUniqueId() != null) {
                    bean.setChiefUniqueId(obu.get().getUniqueId());
                } else {
                    bean.setChiefUniqueId(null);
                }
            }
        }
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_UPDATE, LocateLoginRespDto.class, entity.getTenentId(), entity.getOrgCode(), buildId);
        if (response == null) {
            log.error("部门修改同步异常：response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.error("部门修改同步异常:code= " + entity.getCode() + "--{" + response.getBody().getErrMsg() + "}");
            } else {
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                    log.info("部门修改同步成功 code--" + entity.getCode());
                    flag = true;
                } else {
                    log.error("部门修改同步异常:code= " + entity.getCode());
                }
            }
        }
        log.info("部门修改同步结束........");
        return flag;
    }

    @Override
    public boolean deleteSync(String uniqueId) {
        log.info("部门删除同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门删除同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setUniqueId(uniqueId);
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = null;
        try {
            response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_DELETE, LocateLoginRespDto.class);
        } catch (Exception e) {
            log.error("部门删除同步异常 e{}", e);
        }
        if (response == null) {
            log.error("部门删除同步异常：response is null");
        } else {
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                log.info("部门删除同步成功 uniqueId--" + uniqueId);
                flag = true;
            } else {
                log.info("部门删除同步异常： {}", response.getBody());
            }
        }
        log.info("部门删除同步结束........");
        return flag;
    }

    @Override
    public boolean deleteApiSync(String uniqueId, BuildDataDto dto) {
        log.info("部门删除同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = apiBaseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门删除同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setUniqueId(uniqueId);
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = null;
        try {
            response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_DELETE, LocateLoginRespDto.class, dto);
        } catch (Exception e) {
            log.error("部门删除同步异常 e{}", e);
        }
        if (response == null) {
            log.error("部门删除同步异常：response is null");
        } else {
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                log.info("部门删除同步成功 uniqueId--" + uniqueId);
                flag = true;
            } else {
                log.info("部门删除同步异常： {}", response.getBody());
            }
        }
        log.info("部门删除同步结束........");
        return flag;
    }

    //适用版本：化工4.1及以上版本
    @Override
    public Map<String, String> syncV2ListName() {
        Map<String, String> dMap = new LinkedHashMap<>();
        if (!baseConfigService.checkIfSync()) {
            return dMap;
        }
        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门列表获取异常： 人员在岗在位buildId 未配置。");
            return dMap;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_LIST, LocateLoginRespDto.class);
        if (response == null) {
            log.error("部门列表获取异常同步异常:" + "response is null");
        } else {
            LocateLoginRespDto dto = response.getBody();
            if (StringUtils.isNotBlank(dto.getErrMsg())) {
                log.info("部门列表获取异常同步异常： {}", dto.getErrMsg());
            }
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                List<BpDepartListSyncBean> data = (List<BpDepartListSyncBean>) dto.getData();
                if (CollectionUtils.isNotEmpty(data)) {
                    String str = JSON.toJSONString(data);
                    List<BpDepartListSyncBean> list = JSONObject.parseArray(str, BpDepartListSyncBean.class);
                    Map<Long, BpDepartListSyncBean> map = list.stream().collect(Collectors.toMap(BpDepartListSyncBean::getId, r -> r));
                    for (BpDepartListSyncBean b : list) {
                        String sName = b.getName();
                        String pName = "";
                        if (b.getParentId() != null) {
                            pName = map.get(b.getParentId()).getName();
                        }
                        //将部门名称和父部门名称作为key id作为value
                        dMap.put(sName + "-" + pName, b.getId().toString());
                    }
                }
            }
        }
        return dMap;
    }

    @Override
    public boolean syncDepart(BpDepart depart, BpDepart pDepart) {
        boolean result = false;
        boolean flag = this.getSyncDepart(depart);
        if (flag) {
            result = this.updateSync(depart, pDepart);
        } else {
            result = this.addSync(depart, pDepart);
        }
        return result;
    }

    @Override
    public boolean syncApiDepart(BpDepart depart, BpDepart pDepart, BuildDataDto dto) {
        boolean result = false;
        boolean flag = this.getApiSyncDepart(depart, dto);
        if (flag) {
            result = this.updateApiSync(depart, pDepart, dto);
        } else {
            result = this.addApiSync(depart, pDepart, dto);
        }
        return result;
    }

    @Override
    public boolean syncDepartCross(BpDepart depart, BpDepart pDepart, String buildId) {
        boolean result = false;
        boolean flag = this.getSyncDepartCross(depart, buildId);
        if (flag) {
            result = this.updateSyncCross(depart, pDepart, buildId);
        } else {
            result = this.addSyncCross(depart, pDepart, buildId);
        }
        return result;
    }

    /**
     * @param depart
     * @return
     */
    @Override
    public boolean getSyncDepart(BpDepart depart) {
        boolean flag = false;
        log.info("部门同步查询 Start...UniqueId: " + depart.getUniqueId());
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setUniqueId(depart.getUniqueId());
        bean.setBuildId(buildConfig.getValue());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_SEARCH, LocateLoginRespDto.class);
            if (response == null) {
                log.error("部门同步查询异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.info("部门同步查询异常 : " + response.getBody().getErrMsg());
                }
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && response.getBody().getData() != null) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            log.error("部门同步查询异常 e{}", e);
        }
        log.info("部门同步查询 END... UniqueId: " + depart.getUniqueId());
        return flag;
    }

    @Override
    public boolean getApiSyncDepart(BpDepart depart, BuildDataDto dto) {
        boolean flag = false;
        log.info("部门同步查询 Start...UniqueId: " + depart.getUniqueId());
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = apiBaseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("部门同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setUniqueId(depart.getUniqueId());
        bean.setBuildId(buildConfig.getValue());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_SEARCH, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.error("部门同步查询异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.info("部门同步查询异常 : " + response.getBody().getErrMsg());
                }
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && response.getBody().getData() != null) {
                    flag = true;
                }
            }
        } catch (Exception e) {
            log.error("部门同步查询异常 e{}", e);
        }
        log.info("部门同步查询 END... UniqueId: " + depart.getUniqueId());
        return flag;
    }

    public boolean getSyncDepartCross(BpDepart depart, String buildId) {
        boolean flag = false;
        log.info("部门同步查询 Start...UniqueId: " + depart.getUniqueId());
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
//        BaseConfig buildConfig = baseConfigService.findConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("部门同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpDepartSyncBean bean = new BpDepartSyncBean();
        bean.setUniqueId(depart.getUniqueId());
        bean.setBuildId(buildId);
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_DEPART_SEARCH, LocateLoginRespDto.class, depart.getTenentId(), depart.getOrgCode(), buildId);
        if (response == null) {
            log.error("部门同步查询异常：response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.info("部门同步查询异常 : " + response.getBody().getErrMsg());
            }
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && response.getBody().getData() != null) {
                flag = true;
            }
        }
        log.info("部门同步查询 END... UniqueId: " + depart.getUniqueId());
        return flag;
    }

    //查询公司在定位系统中是否存在
    @Override
    public boolean getSyncCompany(Long tenentId, String orgCode) {
        log.info("公司查询 Start... orgCode={},tenentId={}", tenentId, orgCode);
        boolean flag = false;
        BuildDataDto dto = new BuildDataDto();
        dto.setTenentId(tenentId);
        dto.setOrgCode(orgCode);
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
//        Context.setTenentIdOrgCodeIntercept(false);
        ApiBaseConfigBean buildConfigDto = apiBaseConfigMapper.findConfig(tenentId, orgCode, BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        BaseConfig buildConfig = new BaseConfig();
        BeanUtils.copyProperties(buildConfigDto, buildConfig);
//        Context.setTenentIdOrgCodeIntercept(true);
        if (buildConfig == null) {
            log.info("部门同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpCompanyRequestBean bean = new BpCompanyRequestBean();
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_COMPANY_SEARCH, LocateLoginRespDto.class, tenentId, orgCode, buildConfig.getValue());
        if (response == null) {
            log.error("公司查询异常：response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.info("公司查询异常 : " + response.getBody().getErrMsg());
            }
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && response.getBody().getData() != null) {
                flag = true;
            }
        }
        return flag;
    }

}
